/*River Warren
 *October 15, 2011
 *
 *Inventory.cpp
 *Inventory object to be used by the game Dungeons and Princesses
*/
#include "Inventory.h"
#include "LockableContainer.h"
#include "Monster.h"
#include "Utility.h"

Inventory::Inventory()
{
    numOfItems = 0;
}

//add item to the inventory
void Inventory::add(Base* item)
{
    Listing listing; //prepare to add to the manifest
    //populate the listing struct
    listing.objectID = item->getObjectID();
    listing.description = item->getDesc();
    listing.objectType = item->getObjectType();


    //list the item
    manifest.push_back(listing);
    //inventory the item
    items.insert(make_pair(item->getObjectID(), item));
    ++numOfItems; //increment number of items
}

vector<Listing>const * Inventory::hasObjectInInventory(string itemToLookFor) {
    
    
    return hasObjectInInventory(itemToLookFor, "");
    
}

vector<Listing>const * Inventory::hasObjectInInventory(string itemToLookFor, string objectType) {
    
    vector<Listing>const * playerInventory = getManifest();
    vector<Listing> * inventoryToReturn = new vector<Listing>;
    
    
    string des;
    long found;
    for (int index = 0; index < playerInventory->size(); index++)
    {
        des = playerInventory->at(index).description;
        
        des = Utility::lowerCase(des);
        
        found = des.find(Utility::removeGarbage(itemToLookFor));
        
        //found = des.find("Rusty");
        if (objectType != "") {
            if (found != string::npos && playerInventory->at(index).objectType == objectType)
            {
                inventoryToReturn->push_back(playerInventory->at(index));
            }

        }
        else {
            
            if (found != string::npos)
            {
                inventoryToReturn->push_back(playerInventory->at(index));
            }

        }
    }
    
    return inventoryToReturn;
    
}
//delete item from manifest that has been pulled out of inventory
void Inventory::deleteFromManifest(int thisID)
{
    int i;
    for(i = 0; i < numOfItems && manifest[i].objectID != thisID; ++i); //find item on list
    manifest.erase(manifest.begin() + i); //remove item
    --numOfItems; //decrement number of items
}

void Inventory::saveInventory(ostream& out)
{
    out << numOfItems << endl;
    if(numOfItems > 0)
    {
        string typeList[150];
        int i = 0;
        map<int, Base*>::iterator it; //the guy who looks through the warehouse for the item
        it = items.begin();
        typeList[i] = (*it).second->getObjectType();
        out << typeList[i];
        for(++it; it != items.end(); ++it)
        {
            ++i;
            typeList[i] = (*it).second->getObjectType();
            out << ',' << typeList[i];
        }
        out << endl;
        i = 0;
        for(it = items.begin(); it != items.end(); ++it)
        {
            switch(typeList[i][0])
            {
                case 'E':
                    out << (*(EquipmentType*)it->second) << endl;
                    break;
                case 'K':
                    out << (*(Key*)it->second) << endl;
                    break;
                case 'L':
                    ((LockableContainer*)it->second)->saveLockableContainer(out);
                    break;
                case 'M':
                    out << (*(Monster*)it->second) << endl;
                    break;
                case 'P':
                    out << (*(Potion*)it->second) << endl;
                    break;
            }
            ++i;
        }
    }
}

void Inventory::loadInventory(istream& in)
{
    Key *k = new Key();
    EquipmentType *e = new EquipmentType();
    Potion *p = new Potion();
    LockableContainer *l = new LockableContainer();
    Monster *m = new Monster();
    string typeList[150];
    int i = 0;
    char *tk = NULL;// pointers to chars
    char data[MAX] = ""; //to hold line
    in.getline(data,MAX - 1, '\n');
    int tempNumOfItems = atoi(data);
    if(tempNumOfItems > 0)
    {
        in.getline(data,MAX - 1, '\n');
        tk = strtok(data, DELIMS);  //get first token of new line
        while(tk != NULL)
        {
            typeList[i] = tk;
            tk = strtok(NULL, DELIMS); // get next token
            ++i;
        }
        for(int j = 0; j < i; ++j)
        {
            switch(typeList[j][0])
            {
                case 'E':
                    in >> *e;
                    add(e);
                    break;
                case 'K':
                    in >> *k;
                    add(k);
                    break;
                case 'L':
                    l->loadLockableContainer(in);
                    add(l);
                    break;
                case 'M':
                    in >> *m;
                    add(m);
                    break;
                case 'P':
                    in >> *p;
                    add(p);
                    break;

            }
        }
    }
}
